package com.xx.news.activity;

import android.annotation.TargetApi;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.xx.news.R;
import com.xx.news.base.BaseActivity;
import com.xx.news.entity.Toutiao;

public class WebActivity extends BaseActivity {
    WebView webview;
    ProgressBar progressBar;

    @Override
    public void initData() {
        super.initData();
        webview = findViewById(R.id.webview);
        progressBar = findViewById(R.id.progress_bar);
        Intent intent = getIntent();
        Toutiao.T1348647853363Bean item = (Toutiao.T1348647853363Bean) intent.getSerializableExtra("bean");
//        initWebView();
//        webview.loadUrl(bean.url);
//        webview.loadUrl("https://mini.eastday.com/mobile/210308134708834241845.html");
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    public void initWebView() {
        webview.setHorizontalScrollBarEnabled(false);//水平不显示
        webview.setVerticalScrollBarEnabled(false); //垂直不显示
        WebSettings webSettings = webview.getSettings();//浏览器的web设置信息
        webSettings.setTextZoom(100);//设置字体不随系统字体大小发生变化
        webSettings.setBuiltInZoomControls(false);
        webSettings.setSupportZoom(false);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSettings.setUseWideViewPort(false);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.supportMultipleWindows();  //多窗口
        webSettings.setLoadsImagesAutomatically(true); //支持自动加载图片
        webSettings.setJavaScriptEnabled(true);//设置WebView属性，能够执行Javascript脚本
        webSettings.setDomStorageEnabled(true); // 开启 DOM storage API 功能 设置是否启用app缓存
        webSettings.setDatabaseEnabled(true);   //开启 database storage API 功能
//        webSettings.setAppCacheEnabled(true);//开启 Application Caches 功能
//        showProgressDialog("请稍后...");
        // webView.addJavascriptInterface(new AppWebViewInterface(getActivity(), null, false), "SensorsData_APP_JS_Bridge");
        webview.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                if (TextUtils.isEmpty(title) || title.contains("http")) {
                    return;
                }
                Log.i("webview", "从web上获取到的标题" + title);
//                titleNameTv.setText(title);
            }

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    progressBar.setVisibility(View.GONE);
                    webview.setVisibility(View.VISIBLE);
                } else {
                    progressBar.setVisibility(View.VISIBLE);
                    progressBar.setProgress(newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }
        });
        webview.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                webview.setVisibility(View.GONE);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                webview.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    public int initVariableId() {
        return 0;
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_news_info;
    }
}
